﻿Option Explicit On
Imports Llama
Imports System.Data.SqlClient
Imports System.IO
Imports System.Data.OracleClient
Imports System.Xml
Imports Telerik.WinControls.UI

Module Module1
    Public DS As LMADataSource = Nothing
    Public MySiteNote As String = Nothing
    Public FormList As ArrayList = New ArrayList
    Public DrawingCol As ArrayList = New ArrayList
    Public MaxDepth As Integer
    Public UpdateMode As String
    Public sInsulFilePath As String
    Public m_PlantName As String
    Public objdrawing As LMDrawing
    Public objdrawings As New LMDrawings
    Public strTime As String
    Public objTime As System.DateTime
    Public strUser As String
    Public sFilePath As String
    Public sLogPath As String
    Public strLogPath As String
    Public LogList As ArrayList = New ArrayList
    Public PathMark As String = "\"
    Public sMsg As String
    Public iTotal As Integer

    Public strLogFileName As String = "SEIToollog.txt"
    Public fileStreamWrite As StreamWriter
    Public CONNMARK As String = "  "

    Public SiteSchemaUser As String = Nothing
    Public SiteSchemaUserPS As String = Nothing
    Public SiteDictionaryUser As String = Nothing
    Public SiteDictionaryUserPS As String = Nothing
    Public ActivePlantPIDAppUser As String    'pid application user
    Public ActivePlantPIDAppPS As String
    Public ActivePlantPIDAppServer As String
    Public ActivePlantPIDAppRefUser As String    'pid application user
    Public ActivePlantPIDAppRefPS As String
    Public ActivePlantPIDAppRefServer As String
    Public ActivePlantUser As String    'pid application user
    Public ActivePlantPS As String
    Public ActivePlantServer As String
    Public IfChangeINI As Boolean = False

    Public DBSystemUser As String = Nothing    'DATABASE 系统管理员
    Public DBSystemUserPS As String = Nothing
    Public PlantDBSystemUser As String = Nothing
    Public PlantDBSystemUserPS As String = Nothing
    Public DBTypePID As String = Nothing    'PIDV4.INI
    Public Site_DBNAME As String = Nothing
    Public Plant_DBNAME As String = Nothing
    Public ActiveSiteServer As String = Nothing
    Public DBType As String = Nothing    'INTOOLS.INI
    Public SPIAdminUser As String = Nothing
    Public SPIAdminPS As String = Nothing
    Public strSPFURL As String = Nothing
    Public PlantInSPF As String = Nothing


    ' Public dtRow() As DataRow

    '用于数据库连接
    Public SQLDB As Boolean = False
    Public OraDB As Boolean = False
    Public dtCreate As DataTable = New DataTable
    Public dtCreatePID As DataTable = New DataTable
    Public dtCreateRefPID As DataTable = New DataTable
    Public connSql As SqlConnection = Nothing
    Public conn As OracleConnection = Nothing
    Public SPIconnSql As SqlConnection = Nothing
    Public SPIconn As OracleConnection = Nothing
    Public strConnection As String = Nothing                '配合Sqlconnection使用
    Public commSql As SqlCommand = Nothing
    Public comm As OracleCommand = Nothing
    Public SPIcommSql As SqlCommand = Nothing
    Public SPIcomm As OracleCommand = Nothing
    Public SelectStr As String = Nothing                    '配合adapter使用
    Public adpsql As SqlDataAdapter = Nothing
    Public adp As OracleDataAdapter = Nothing
    Public SPIadpsql As SqlDataAdapter = Nothing
    Public SPIadp As OracleDataAdapter = Nothing
    Public Reset_User As String = Nothing
    Public Reset_Password As String = Nothing

    '用于记录DBClean的ID
    Public sValidModelID As String = ""
    Public sUValidModelID As String = ""
    Public iValidID As Integer = 0
    Public iUValidID As Integer = 0
    Public isOK As Boolean = False

    ''用于SSK打开.pid文件
    Public sskApp As Ingr.RAD2D.Application
    Public sskWrapperApp As Ingr.RAD2D.WrapperApplication




    'Public Sub Writelog(msg As String, mode As String)
    '    Dim obj As Object = New Object
    '    Try

    '        Dim logPosition As String = sFilePath & strLogFileName

    '        SyncLock (obj)
    '            Using file As System.IO.StreamWriter = New System.IO.StreamWriter(logPosition, True)  '若不存在 会 自动 创建一个。但是上级文件夹需要有
    '                file.WriteLine(DateTime.Now.ToLocalTime().ToString())
    '                file.WriteLine(msg + Environment.NewLine)
    '                file.Close()
    '            End Using
    '        End SyncLock

    '    Catch ex As Exception
    '        '免得文件夹路径没有而报错
    '    End Try
    '    obj = Nothing
    'End Sub
    Public Sub ExcuteLog(LogList As ArrayList)
        Dim strTime As String
        Dim objTime As System.DateTime
        Dim objText As System.IO.StreamWriter
        Dim strUser As String
        Dim i As Integer
        Dim strTempPath As String
        Dim g_LogPath As String
        Dim m As Integer
        Dim n As Integer
        Dim temp As String


        objTime = Now
        strTime = Now.ToString
        strUser = Environment.GetEnvironmentVariable("USERNAME")

        sFilePath = "C:\Users\" & strUser & "\Desktop\"

        strTempPath = System.Environment.GetEnvironmentVariable("TEMP")
        g_LogPath = sFilePath & "SEIToollog"
        If IO.Directory.Exists(g_LogPath) Then
            strLogPath = g_LogPath & PathMark & m_PlantName & ".log"
            'If IO.File.Exists(strLogPath) = False Then
            '    objText = IO.File.CreateText(strLogPath)
            'End If
        Else
            strLogPath = strTempPath & PathMark & m_PlantName & ".log"
            'MsgBox(g_LogPath & "此路径不存在或者没有权限，日志已写入本地Temp文件夹下")
        End If

        objText = New System.IO.StreamWriter(strLogPath, True, System.Text.Encoding.UTF8)

        For m = LogList.Count - 1 To 0 Step -1
            If InStr(LogList.Item(m), "Error") > 0 Then
                temp = LogList.Item(m)
                LogList.RemoveAt(m)
                LogList.Add(temp)
            End If
        Next
        For i = 0 To LogList.Count - 1
            objText.WriteLine(strUser & CONNMARK & strTime & CONNMARK & LogList.Item(i))
        Next
        objText.Close()
        LogList.Clear()
    End Sub


    Public Sub RunCMD(str As String)

        System.Threading.Thread.Sleep(500)  ' 程序沉睡0.5 秒
        Dim myProcess As Process
        Dim strIn As IO.StreamWriter
        myProcess = New Process     '打开 cmd进程
        myProcess.StartInfo.FileName = "cmd.exe"
        myProcess.StartInfo.UseShellExecute = False
        myProcess.StartInfo.CreateNoWindow = True ''是否显示DOS窗口，true代表隐藏;
        myProcess.StartInfo.RedirectStandardInput = True
        myProcess.StartInfo.RedirectStandardOutput = True
        myProcess.StartInfo.RedirectStandardError = True
        myProcess.Start()

        strIn = myProcess.StandardInput
        'Dim strOut As StreamReader = myProcess.StandardOutput
        'Dim strErr As StreamReader = myProcess.StandardError
        strIn.AutoFlush = True

        strIn.Write(str + System.Environment.NewLine)
        'strIn.Write("net user snec1 /add" + System.Environment.NewLine)              '创建User用户

        strIn.Close()
        myProcess.Close()
        myProcess = Nothing
        strIn = Nothing
    End Sub


    'Public Sub RefreshProcessBar(RadProgressBarElement1 As RadProgressBarElement, RadStatusStrip1 As RadStatusStrip, iIndex As Integer, iTotal As Integer, sMsg As String)

    '    MainForm.RadLabelElement1.Text = sMsg
    '    If iTotal <> 0 Then
    '        RadProgressBarElement1.Value1 = (iIndex / iTotal) * 100
    '    ElseIf iIndex = 0 And iTotal = 0 And sMsg = "" Then
    '        RadProgressBarElement1.Value1 = 0
    '    End If

    '    RadStatusStrip1.StatusBarElement.UpdateLayout()
    '    RadStatusStrip1.Refresh()
    'End Sub
    Public Sub RefreshBorderProcessBar(RadProgressBarElement1 As RadProgressBarElement, RadStatusStrip1 As RadStatusStrip, iIndex As Integer, iTotal As Integer, sMsg As String, sName As String)

        MainForm.RadLabelElement1.Text = sMsg
        If iTotal <> 0 Then
            RadProgressBarElement1.Value1 = (iIndex / iTotal) * 100
            MainForm.RadLabelElement2.Text = sName
        ElseIf iIndex = 0 And iTotal = 0 And sMsg = "" Then
            RadProgressBarElement1.Value1 = 0
        End If

        RadStatusStrip1.StatusBarElement.UpdateLayout()
        RadStatusStrip1.Refresh()
    End Sub
    Public Sub RefreshProcessBar(RadProgressBarElement1 As RadProgressBarElement, RadStatusStrip1 As RadStatusStrip, iIndex As Integer, iTotal As Integer, sMsg As String, sName As String)

        MainForm.RadLabelElement3.Text = sMsg
        If iTotal <> 0 Then
            RadProgressBarElement1.Value1 = (iIndex / iTotal) * 100
            MainForm.RadLabelElement4.Text = sName
        ElseIf iIndex = 0 And iTotal = 0 And sMsg = "" Then
            RadProgressBarElement1.Value1 = 0
        End If

        RadStatusStrip1.StatusBarElement.UpdateLayout()
        RadStatusStrip1.Refresh()
    End Sub


End Module
